/////////////////////////////////////////////////////////////////////////////
// Name:        CategoryListBox.cpp
// Purpose:     
// Author:      yospaly
// Modified by: 
// Created:     11/12/2008 21:43:23
// RCS-ID:      
// Copyright:   Copyright (C) 2008, yospaly
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "CategoryListBox.h"
#include "events.h"
#include "inibuilder.h"

////@begin XPM images
////@end XPM images


/*!
 * CategoryListBox type definition
 */

IMPLEMENT_DYNAMIC_CLASS( CategoryListBox, wxListBox )


/*!
 * CategoryListBox event table definition
 */

BEGIN_EVENT_TABLE( CategoryListBox, wxListBox )

////@begin CategoryListBox event table entries
    EVT_LISTBOX( ID_CATEGORY_LISTBOX, CategoryListBox::OnSelected )

////@end CategoryListBox event table entries

END_EVENT_TABLE()


/*!
 * CategoryListBox constructors
 */

CategoryListBox::CategoryListBox()
{
    Init();
}

CategoryListBox::CategoryListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator)
{
    Init();
    Create(parent, id, pos, size, choices, style, validator);
}


/*!
 * CategoryListBox creator
 */

bool CategoryListBox::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator)
{
////@begin CategoryListBox creation
    wxListBox::Create(parent, id, pos, size, choices, style, validator);
    CreateControls();
////@end CategoryListBox creation
    return true;
}


/*!
 * CategoryListBox destructor
 */

CategoryListBox::~CategoryListBox()
{
////@begin CategoryListBox destruction
////@end CategoryListBox destruction
}


/*!
 * Member initialisation
 */

void CategoryListBox::Init()
{
////@begin CategoryListBox member initialisation
////@end CategoryListBox member initialisation
}


/*!
 * Control creation for CategoryListBox
 */

void CategoryListBox::CreateControls()
{    
////@begin CategoryListBox content construction
    this->SetFont(wxFont(10, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("Verdana")));
////@end CategoryListBox content construction
    
    default_items = this->GetStrings();
	this->SetSelection(0);
}


void CategoryListBox::ShowCategories() {
    IniBuilder& inibuilder = IniBuilder::get();
    
    this->Clear();
    this->Append(this->default_items);
    
    for( int i=0; i<inibuilder.cat_names.GetCount(); i++ ) {
    	wxString cat = inibuilder.cat_names[i];
    	int count = inibuilder.categories[cat].pkg_names.GetCount();
    	
    	this->Append(wxString::Format(_T("%s (%d)"), cat.c_str(), count));
    }
    
	this->SetSelection(0);
}


/*!
 * wxEVT_COMMAND_LISTBOX_SELECTED event handler for ID_CATEGORY_LISTBOX
 */

void CategoryListBox::OnSelected( wxCommandEvent& event )
{
    const wxString& cat = this->GetStringSelection();
    
    /* if (0==cat.CmpNoCase(_T("All")) || 0==cat.CmpNoCase(_T("Installed"))
    	|| 0==cat.CmpNoCase(_T("Upgradable")) || 0==cat.CmpNoCase(_T("Not Installed")) ) {
    	EventCenter::post(wxEVT_PKG_FILTER_EVENT, cat);
    } else {
		int i = cat.Find(_T(' '));
		if (i!=wxNOT_FOUND) {
			EventCenter::post(wxEVT_PKG_FILTER_EVENT, cat.SubString(0,i-1));
			// EventCenter::post(wxEVT_PKG_FILTER_EVENT, _T("All"));
		}
    }*/
    
    if ( cat.StartsWith(_T("Mark for ")) ) {
    	EventCenter::post(wxEVT_PKG_FILTER_EVENT, cat);
    } else { // remove package count number
		int i = cat.Find(_T(' '));
		if (i!=wxNOT_FOUND) {
			EventCenter::post(wxEVT_PKG_FILTER_EVENT, cat.SubString(0,i-1));
		} else {
			EventCenter::post(wxEVT_PKG_FILTER_EVENT, cat);
		}
    }
}

